package com.example.mybaseapp.widget.circle_seekbar

import android.content.Context
import android.graphics.PointF
import android.util.TypedValue
import androidx.annotation.Dimension
import androidx.annotation.Dimension.Companion.DP
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.sin

/**
 * 刻度盘 绘制类 的帮助类
 * 帮忙计算一些坐标、角度
 */
class DrawaUIHelper {
    companion object{
        fun dpToPx(context: Context, @Dimension(unit = DP) dp: Float): Float {
            val r = context.resources
            return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                r.displayMetrics
            )
        }
    }
    /**指针的三角形的顶部角度的一半度数*/
    val smallAngleChange: Float = 10F
    /**刻度间距之间的角度差*/
    val stepSclaeDegree = 7.5F
    /**刻度的数量*/
    val scaleSize = 360/stepSclaeDegree.toInt()

    /**
     * 获取指针顶部三角形的底部两个坐标
     */
    fun calculatePoints(p: PointF, angle: Float,radius:Float):Pair<PointF,PointF>{
        val point1  = extendPointOnCircle(p, (angle+smallAngleChange), radius)
        val point2  = extendPointOnCircle(p, (angle-smallAngleChange), radius)
        return Pair(point1, point2)
    }

    /**
     * 通过圆心、角度、半径，获得坐标
     * 获取三角形顶部的坐标
     * @param distance 半圆+三角形的长度
     */
    fun extendPointOnCircle(point:PointF, angle: Float, distance: Float): PointF {
        val extendedX = point.x + distance * cos(Math.toRadians(angle.toDouble()))
        val extendedY = point.y + distance * sin(Math.toRadians(angle.toDouble()))
        return PointF(extendedX.toFloat(), extendedY.toFloat())
    }

    fun extendPointOnCircle(angle: Float, distance: Float): PointF {
        val extendedX =  distance * cos(Math.toRadians(angle.toDouble()))
        val extendedY =  distance * sin(Math.toRadians(angle.toDouble()))
        return PointF(extendedX.toFloat(), extendedY.toFloat())
    }



    /**
     * 根据当前的角度，得到所有刻度的角度 以中间轴为对称，顺时针赋值刻度[多的放左边]
     * @param startDegree 起始角度
     * @param pointsData 刻度数据
     * 这里写死的以-90度开始计算两边各自的角度，也就是显示居中的角度
     *
     */
    fun getAllPoints(pointsData: MutableList<PointsData>){
        var middleIndex = (pointsData.size - 1) / 2
        if(middleIndex%2!=0){
            middleIndex+=1
        }

        for (d in 0 until middleIndex + 1) {
                pointsData[middleIndex - d].degree = -90F - d * stepSclaeDegree

            if (d + middleIndex + 1 < pointsData.size) {
                    pointsData[d + middleIndex + 1].degree = -90F + (d + 1) * stepSclaeDegree
            }
        }
    }

    /**
     * 校准角度
     * @param degree 当前的角度
     */
    fun adjustSelect(degree:Float):Float{
        val halfStep = stepSclaeDegree / 2

        val diff = (degree + 360) % 360
        val adjustedDiff = if (diff > 180) diff - 360 else diff

        val quotient = (adjustedDiff / stepSclaeDegree).toInt()
        val remainder = abs(adjustedDiff % stepSclaeDegree)//步长要的是绝对值

        return if (remainder > halfStep) {
            if(quotient<0){
                ((quotient - 1) * stepSclaeDegree)
            }else {
                ((quotient + 1) * stepSclaeDegree)
            }
        } else {
            (quotient * stepSclaeDegree)
        }
    }

}